<?php

//预约表模型

class AppointModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'user_appoint';
	}
	//分页列表
	function get_list($cid,$date){
	
		$where = " ua.cid = '{$cid}' AND ua.sdate='{$date}' AND ua.status !=2";

		$sql  = "select ua.id,ua.sdate,ua.stime,ua.num,ua.status,u.nickname,u.mobile,u.headimg,coach.nickname as coach_name,coach.id as coach_id from ".$this->db->dbprefix.$this->table." as ua 
				 LEFT JOIN ".$this->db->dbprefix."user AS u ON ua.user_id = u.id 
				 LEFT JOIN ".$this->db->dbprefix."coach AS coach ON ua.mid = coach.id 
				 where {$where} limit 100";
		$query = $this->db->query($sql);
		$res = $query->result_array();
		
		return $res;		
	}

	//分页总数
	function get_count($cid,$date,$stime){		
	
		$where = " cid = '{$cid}' AND sdate='{$date}' AND stime='{$stime}' AND status !=2";
		
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table." where {$where} limit 1";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		
		return $row['num']?$row['num']:0;		
	}
	//
	function time_is_exits($cid,$date,$uid,$stime){
		
		$where = " cid = '{$cid}' AND sdate ='{$date}' AND user_id='{$uid}' AND stime='{$stime}' AND status !=2";
		
		$sql  = "select id from ".$this->db->dbprefix.$this->table." where {$where} limit 1";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		
		return $row?1:0;
	}
	
	function add_data($arr){
		$insert_id = $this->add($arr);
		$time = time();
		$logarr = array(
			'user_id'=>$arr['user_id'],
			'uc_id'=>$insert_id,
			'addtime'=>$time ,
			'num'=>1
		);
		
		$this->load->model('Yuyue/CardLogModel','logs');
		$this->logs->add($logarr);
		
		return true;
	}
	
	function get_info($cid,$id){
		$info = $this->_id_get_info($id);
		if(!$info){
			return false;
		}
		
		
		return $info;
	}
	
}

